package com.zxp.cloud.order.service.impl;

import com.zxp.cloud.order.feign.StorageFeign;
import com.zxp.cloud.order.model.Order;
import com.zxp.cloud.order.repository.OrderRepository;
import com.zxp.cloud.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @author zxp
 * @date 2021/7/19 17:16
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private StorageFeign storageFeign;



    //@Transactional
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public Order createOrder(Long userId,Long productId, Integer count) {

        Order order = new Order();
        order.setCount(count);
        order.setProductId(productId);
        order.setUserId(userId);
        order.setStatus(0);
        order.setCreateTime(new Date());
        orderRepository.save(order);

        // 减少库存
        storageFeign.descrement(productId,count);


        return order;
    }

    @Override
    public Order save(Order order) {
        return orderRepository.save(order);
    }
}
